﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.CTTinhDiemMonHocService;
namespace eTMS.Controllers{
public class CTTinhDiemMonHocControllerBase : CommonController{
	public CTTinhDiemMonHocControllerBase()
		: base()
	{	
		this.dao = new CTTinhDiemMonHocServiceClient();
	}
	~CTTinhDiemMonHocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private CTTinhDiemMonHocEntity oCTTinhDiemMonHoc;	
	public CTTinhDiemMonHocEntity CTTinhDiemMonHocObject{
		get{
			return oCTTinhDiemMonHoc;
		}
		set{
			oCTTinhDiemMonHoc=value;
		}
	}
	protected CTTinhDiemMonHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSCTTinhDiemMonHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSCTTinhDiemMonHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oCTTinhDiemMonHoc==null)return false;
		bool result=true;
		try{
			oCTTinhDiemMonHoc = dao.SaveRet(oCTTinhDiemMonHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListCTTinhDiemMonHoc.Add(oCTTinhDiemMonHoc);
		}
		return result;
	}
	public bool Update(){
		if(oCTTinhDiemMonHoc==null)return false;
		return dao.Update(oCTTinhDiemMonHoc);
	}
	public bool Delete(){
		if(oCTTinhDiemMonHoc==null)return false;
		bool retvalue = dao.Delete(oCTTinhDiemMonHoc);
		if(retvalue){			
			_ListCTTinhDiemMonHoc.Remove(oCTTinhDiemMonHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 CTTinhDiemMonHoc_ID){
		bool retvalue = dao.DeleteByID(CTTinhDiemMonHoc_ID);		
		if(retvalue){
			CTTinhDiemMonHocEntity oDeleted=FindCTTinhDiemMonHoc(CTTinhDiemMonHoc_ID);
			if(oDeleted!=null){
				_ListCTTinhDiemMonHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oCTTinhDiemMonHoc = new CTTinhDiemMonHocEntity();
	}
	//Check if exists MaCTTinhDiem
	public bool CheckExistsMaCTTinhDiem(object value){	
		return dao.SelectOneByMaCTTinhDiem(value.ToString()).MaCTTinhDiem!=null;
    }    
    //Copy data from data row in grid to object
    public CTTinhDiemMonHocEntity MapDataRowToObject(DataRow row){
		CTTinhDiemMonHocEntity o = new CTTinhDiemMonHocEntity();
		 o.MaCTTinhDiem=Convert.ToString(row["MaCTTinhDiem"]);
		 o.TenCTTinhDiem=Convert.ToString(row["TenCTTinhDiem"]);
		 if(!row.IsNull("SuDungMacDinh"))
			o.SuDungMacDinh=ConvertBool(row["SuDungMacDinh"]);
		 if(!row.IsNull("CTTinhDiemMonHoc_ID"))
		    o.CTTinhDiemMonHoc_ID=(Int32)(row["CTTinhDiemMonHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(CTTinhDiemMonHocEntity o,DataRow row){
		row["MaCTTinhDiem"]=o.MaCTTinhDiem;
		row["TenCTTinhDiem"]=o.TenCTTinhDiem;
		row["SuDungMacDinh"]=o.SuDungMacDinh;
		row["CTTinhDiemMonHoc_ID"]=o.CTTinhDiemMonHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.CTTinhDiemMonHocObject,row);
    }
	public void LoadObject(Int32 CTTinhDiemMonHoc_ID){
		CTTinhDiemMonHocEntity oLoad=FindCTTinhDiemMonHoc(CTTinhDiemMonHoc_ID);
		if(oLoad==null){
			oCTTinhDiemMonHoc=this.LoadCTTinhDiemMonHoc(CTTinhDiemMonHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oCTTinhDiemMonHoc.IsNullCTTinhDiemMonHoc_ID;
			if(!bNotIsNull){
				this.ListCTTinhDiemMonHoc.Add(oCTTinhDiemMonHoc);
			}
		}
		else{
			oCTTinhDiemMonHoc=oLoad;
		}
	}
	#region CachedList CTTinhDiemMonHoc
	private static List<CTTinhDiemMonHocEntity> _ListCTTinhDiemMonHoc=new List<CTTinhDiemMonHocEntity>();
    public List<CTTinhDiemMonHocEntity>ListCTTinhDiemMonHoc{
		get{
			return _ListCTTinhDiemMonHoc;
		}
	}
	public static List<CTTinhDiemMonHocEntity>ListCTTinhDiemMonHocStatic{
		get{
			return _ListCTTinhDiemMonHoc;
		}
	}
	public CTTinhDiemMonHocEntity FindCTTinhDiemMonHoc(Int32 CTTinhDiemMonHoc_ID){
		CTTinhDiemMonHocEntity oResult=this.ListCTTinhDiemMonHoc.Find(delegate(CTTinhDiemMonHocEntity oFind){
			bool result=true; 
			result&=oFind.CTTinhDiemMonHoc_ID.Equals(CTTinhDiemMonHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllCTTinhDiemMonHoc(bool clearCache){
		if(_ListCTTinhDiemMonHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		CTTinhDiemMonHocServiceClient dao = new CTTinhDiemMonHocServiceClient();
		_ListCTTinhDiemMonHoc=new List<CTTinhDiemMonHocEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public CTTinhDiemMonHocEntity LoadCTTinhDiemMonHoc(Int32 CTTinhDiemMonHoc_ID){			
		CTTinhDiemMonHocEntity o=dao.SelectOne(CTTinhDiemMonHoc_ID);		
		return o;
	}
	#endregion
}
}
